import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Role, RoleParam } from './model';
import type { Menu } from '../menu/model';

/**
 * 分页查询角色
 */
export async function pageRoles(params: RoleParam) {
  const res = await request.post<ApiResult<PageResult<Role>>>('/sysRole/page', {
    params
  });

  if (res.data.code === 200) {
    return res.data.result;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询角色列表
 */
export async function listRoles(params?: RoleParam) {
  const res = await request.get<ApiResult<Role[]>>('/sysRole/list', {
    params
  });
  if (res.data.code === 200 && res.data.result) {
    return res.data.result;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加角色
 */
export async function addRole(data: Role) {
  const res = await request.post<ApiResult<unknown>>('/sysRole/add', data);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改角色
 */
export async function updateRole(data: Role) {
  const res = await request.post<ApiResult<unknown>>('/sysRole/update', data);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除角色
 */
export async function removeRole(id: number | string) {
  const res = await request.post<ApiResult<unknown>>('/sysRole/delete', {
    id
  });
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除角色
 */
export async function removeRoles(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>('/system/role/batch', {
    data
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取角色分配的菜单
 */
export async function listRoleMenus(roleId?: number) {
  const res = await request.get<ApiResult<Menu[]>>(
    '/system/role-menu/' + roleId
  );
  if (res.data.code === 0) {
    return res.data.result;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改角色菜单
 */
export async function updateRoleMenus(params: any) {
  const res = await request.post<ApiResult<unknown>>(
    '/sysRole/grantMenu',
    params
  );
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改角色
 */
export async function setRoleStatus(id: number, status: number) {
  const res = await request.post<ApiResult<unknown>>('/sysRole/setStatus', {
    id,
    status
  });
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改角色
 */
export async function ownMenuList(id: number | string) {
  const res = await request.get<ApiResult<any[]>>('/sysRole/ownMenu', {
    params: { id }
  });
  if (res.data.code === 200) {
    return res.data.result;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取角色拥有机构列表(数据范围)
 */
export async function ownOrgList(id?: number) {
  const res = await request.get<ApiResult<any[]>>('/sysRole/ownOrg', {
    params: { id }
  });
  if (res.data.code === 200) {
    return res.data.result;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 授权角色数据
 */
export async function grantRoleData(params?: any) {
  const res = await request.post<ApiResult<object>>(
    '/sysRole/grantData',
    params
  );
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
